package com.java.util;

import com.java.util.pojo.User;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
 * list集合分组相关
 */
public class ListGroupMapList {

    public static void main(String[] args) {

        List<User> list = new ArrayList<>();


        list.add(new User(1, "ss", "上海", 222,true));
        list.add(new User(1, "qq", "上海", 222,true));
        list.add(new User(1, "ww", "上海", 222,true));
        list.add(new User(2, "ee", "上海", 333,true));
        list.add(new User(1, "rr", "上海", 333,true));


        // toMap 可以使用
        // keyMapper Function接口 传入一个值，返回一个值。是map中key的生成策略
        // valueMapper Function接口 传入一个值，返回一个值。是map中value的值
        // mergeFunction BinaryOperator本质是一个BiFunction接口，传入两个值返回一个值，
        // 即：对于相同key传入的value值，如何做合并。
        Collector<User, ?, Map<Object, User>> userMapCollector = Collectors.toMap(
                e -> {

                    return e.getId();
                },
                e -> {
                    return e;
                },
                new BinaryOperator<User>() {
                    @Override
                    public User apply(User o, User o2) {
                        return o;
                    }
                }
        );

        // 基于某个值进行分组，Collectors.groupingBy
        // Collectors.collectingAndThen收集toMap分组的Map结果中value的集合，转为List集合
        Map<Integer, List<User>> collect = list.stream().collect(Collectors.groupingBy(
                new Function<User, Integer>() {
                    @Override
                    public Integer apply(User user) {
                        return user.getAssetId();
                    }
                },
                Collectors.collectingAndThen(userMapCollector
                        ,
                        map -> {

                            return new ArrayList<>(map.values());
                        }
                )
        ));

        System.out.println(collect);

        list.stream().forEach(System.out::println);
    }



    /**
     * 这个是Map-List<数据类型>
     */
    public static void mapList() {
        List<User> list = new ArrayList<>();

        list.add(new User(1, "ss", "上海", 222, true));
        list.add(new User(1, "qq", "上海", 222, true));
        list.add(new User(1, "ww", "上海", 222, true));
        list.add(new User(2, "ee", "上海", 333, true));
        list.add(new User(1, "rr", "上海", 333, true));


        Map<Integer, List<Integer>> collect = list.stream()
                .collect(Collectors.groupingBy(
                        // 按assetId分组
                        User::getAssetId,
                        // 收集每个分组中User对象的id属性到List中
                        Collectors.mapping(User::getId, Collectors.toList())
                ));

        collect.keySet().forEach(k -> System.out.println(k + " >>>>> " + collect.get(k)));

        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ");
        list.forEach(System.out::println);
    }
}
